package towa.com.remoteWMP;

import java.io.IOException;
import java.net.SocketTimeoutException;
import java.nio.channels.SocketChannel;

import android.app.Service;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

public class CommunicationService extends Service {
	private static final String PREFS_NAME = "WMPRemotePrefs";
	private final IBinder mBinder = new LocalBinder();
//	private RemoteWMPActivity activity;
	private ComThread thread;
	private ReceiveThread thread2;
	private SocketChannel socket;
	private String address;
	private int port;
	private boolean startup;

		
	@Override
	public void onCreate(){
		super.onCreate();
		Log.i("WMPRemote", "Service onCreate");
		SharedPreferences settings = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
		address = settings.getString("address", "192.168.0.184");
		port = settings.getInt("port", 1445);
		
		if (thread == null) {
			thread = new ComThread();		
			try {
				socket = thread.initSocket(address, port);
				thread.start();
			} catch (SocketTimeoutException e) {
				newDataReceived("FAIL!");
			} catch (IOException e) {
				Log.e("WMPRemoteConnection", e.toString());
			}
		}
		
		if (socket != null) {
			if (thread2 == null) {
				thread2 = new ReceiveThread(this, socket.socket());
				thread2.start();
			}
			try {
				Thread.sleep(300);
				thread.sendData(socket, "playerInfo;get;<EOS>");
				startup = true;
//				thread.sendData(socket, "playlist;get;<EOS>");
			} catch (IOException e) {
				newDataReceived("FAIL!");
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	@Override
	public void onDestroy(){
		super.onDestroy();
		thread = null;
		thread2.disableRunning();
		try {
			socket.close();
		} catch (IOException e) {
			Log.e("WMPRemote", e.toString());
		}
	}
	
	public class LocalBinder extends Binder{
		CommunicationService getService(){
			return CommunicationService.this;
		}
	}
	
	@Override
	public IBinder onBind(Intent arg0) {
		return mBinder;
	}
		
	public synchronized void sendData(String dataString){
		try {
			thread.sendData(socket, dataString);
		} catch (IOException e) {
			newDataReceived("FAIL!");
		}		
	}
	
	public void retry() throws IOException{
			socket = thread.initSocket(address, port);
			thread2 = new ReceiveThread(this, socket.socket());
			thread2.start();
	}
	
	public synchronized void newDataReceived(String data){
		Intent broadcastIntent = new Intent();
		broadcastIntent.setAction("WMPRemote");
		broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT);
		broadcastIntent.putExtra("data", data);
		sendBroadcast(broadcastIntent);
		Log.i("WMPRemote", data);
		if(startup){
			try {
				thread.sendData(socket, "playlist;get;<EOS>");
				startup = false;
			} catch (IOException e) {}
		}
	}
	
	public void reconnect(String tempAddress, int tempPort){
		if (tempAddress != null && socket != null && thread2 != null) {
			address = tempAddress;
			port = tempPort;
			try {
				socket.close();
				thread2.disableRunning();
				thread2 = null;
			} catch (IOException e) {
				Log.e("WMPRemote", e.toString());
			}
			try {
				socket = thread.initSocket(address, tempPort);
			} catch (IOException e) {
				newDataReceived("FAIL!");
			}
			thread2 = new ReceiveThread(CommunicationService.this, socket.socket());
			thread2.start();
			Log.i("WMPRemote", "Address: " + tempAddress + " " + address);
		}
	}

}
